package com.hxkj.client.service.taglib.directive;

import com.hxkj.client.service.NavigationServiceClient;
import com.hxkj.domain.context.EnvironmentContextHolder;
import com.hxkj.domain.po.Navigation;
import com.hxkj.domain.po.NavigationTree;
import com.hxkj.domain.po.WebsiteAddr;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import static com.hxkj.client.service.taglib.config.LableConstant.CMS_NAVIGATION;

/**
 * @ClassName 导航栏 自定义标签
 * @Description
 * @Author chenfangzhou
 * @Date 2018/7/18 16:14
 * @Version 1.0
 */
@Component
@Slf4j
public class NavigationDirective extends AbstractDirective {
    @Autowired
    private NavigationServiceClient navigationServiceClient;
    //站点id
    private static final String SITE_ID = "siteId";
    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
        try {
            Long siteId = getLong(SITE_ID, params) == null ? (Long) getCustomAttrbute(env, SITE_ID) : getLong(SITE_ID, params);
            String note = super.getNote(env);
            // 导航栏
            Navigation navigation = new Navigation();
            navigation.setSiteId(siteId);
            navigation.setIsEnable(0);

//            // 查询redis缓存
//            List<NavigationTree> navigationTreeList = (List<NavigationTree>) redisTemplate.boundValueOps("directive_" + siteId + CMS_NAVIGATION + "_dailyArticleList").get();
//            if (null == navigationTreeList) {
//                navigationTreeList = navigationServiceClient.getNavigationList(navigation).getData();
//
//                redisTemplate.boundValueOps("directive_" + siteId + CMS_NAVIGATION + "_dailyArticleList").set(navigationTreeList);
//            }
            List<NavigationTree> navigationTreeList = navigationServiceClient.getNavigationList(navigation).getData();
            WebsiteAddr webSiteAddr = getWebSiteAddr(siteId);
            if (StringUtils.isNotBlank(note)) {
                for (NavigationTree navigationTree : navigationTreeList) {
                    navigationTree.setManuscriptPath(webSiteAddr.getIpAdd() + navigationTree.getManuscriptPath());
                }
            }else{
                // 导航栏静态文件链接加上下文
                Map<String, Object> enviroment = EnvironmentContextHolder.getEnvironment();
                Map<Long, String> siteCtxMap = (Map<Long, String>) enviroment.get("siteCtxMap");
                for (NavigationTree item : navigationTreeList) {
                    String urlContext = siteCtxMap.get(siteId);
                    if(StringUtils.isNotBlank(urlContext)) {
                        //导航栏用的是NavigationUrl属性
//                        item.setManuscriptPath(urlContext + item.getManuscriptPath());
                        //导航栏地址如果不是http开头,判断为内部相对路径,添加上上下文
                        if(!item.getNavigationUrl().startsWith("http")) {
                            item.setNavigationUrl(urlContext + item.getNavigationUrl());
                        }
                    }
                }
            }
            env.setVariable("navigationTreeList", DefaultObjectWrapperBuilderFactory.getDefaultObjectWrapper().wrap(navigationTreeList));
            if (body != null) {
                body.render(env.getOut());
            } else {
                log.debug("导航栏所在模板至少要加一个空格");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
